package cn.droidlover.xdroidmvp.mvp;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import cn.droidlover.xdroidmvp.R;
import cn.droidlover.xdroidmvp.XDroidConf;
import cn.droidlover.xdroidmvp.base.BaseDelegate;
import cn.droidlover.xdroidmvp.util.ScreenUtils;
import cn.droidlover.xdroidmvp.util.StatusBarUtil;

/**
 *
 */
public class BaseActivity extends RxAppCompatActivity  {

    protected BaseDelegate mBaseDelegate;

    protected Activity context;

    protected int screenWidth;

    private Fragment currentFragment;

    private RxPermissions rxPermissions;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        screenWidth = ScreenUtils.getWindowWidth(context);
        final BaseDelegate delegate = getBaseDelegate();
        delegate.onCreate(savedInstanceState);

    }

    @NonNull
    public BaseDelegate getBaseDelegate() {
        if (mBaseDelegate == null) {
            mBaseDelegate = new BaseDelegate(this);
        }
        return mBaseDelegate;
    }


    protected void setStatusBlack() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        StatusBarUtil.StatusBarLightMode(this);
    }

    protected void setStatusBlack(boolean flag) {
        if(flag){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |View.SYSTEM_UI_FLAG_VISIBLE);
        }
        StatusBarUtil.StatusBarLightMode(this,flag);
    }


    //需要弹出Dialog的时候用这个，底部是透明色
    protected void setStatusColor(boolean status) {
        if (status) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  //如果sdk版本大于21
                View decorView = getWindow().getDecorView();
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        }
    }

    protected void setStatusColor(boolean status, int color) {
        if (status) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  //如果sdk版本大于21
                View decorView = getWindow().getDecorView();
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                if (color == 1) {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.color_white));
                } else if (color == 2) {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.color_300000));
                }else if(color == 3){
                    getWindow().setStatusBarColor(getResources().getColor(R.color.color_e3e3e3));
                }else if(color == 4){
                    getWindow().setStatusBarColor(getResources().getColor(R.color.pop_banner_ground));
                }
                else if(color == 5){
                    getWindow().setStatusBarColor(getResources().getColor(R.color.color_ECECEC));
                }

            }
        }
    }


    @Override
    protected void onResume() {
        getBaseDelegate().onResume();
        super.onResume();
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getBaseDelegate().attachBaseContext(newBase));
    }

    /**
     * 这个可以视情况 重写 (当横竖屏等配置发生改变时)
     *
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getBaseDelegate().onConfigurationChanged(newConfig);
    }

    /**
     * 设置横屏竖屏
     *
     * @param mOrientationPortrait true 竖屏 false 横屏
     */
    public void setOrientationPortrait(boolean mOrientationPortrait) {
        getBaseDelegate().setOrientationPortrait(mOrientationPortrait);
    }

    public boolean isOrientationPortrait() {
        return getBaseDelegate().mOrientationPortrait;
    }



    /**
     * 切换 fragment
     *
     * @param targetFragment
     * @param containerId
     * @return
     */
    protected void switchFragment(int containerId, Fragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            //第一次使用switchFragment()时currentFragment为null，所以要判断一下
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(containerId, targetFragment, targetFragment.getClass().getName());
        } else {
            transaction.hide(currentFragment).show(targetFragment);
        }
        currentFragment = targetFragment;
        transaction.commitAllowingStateLoss();
    }


    protected RxPermissions getRxPermissions() {
        rxPermissions = new RxPermissions(this);
        rxPermissions.setLogging(XDroidConf.DEV);
        return rxPermissions;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBaseDelegate = null;
    }
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.new_push_left_in, R.anim.new_push_left_out);// 从右向左推出动画效果
    }



}
